﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Xml;
using Axis.Dao.Research;
using Axis.Web.Administrator.Global;
using Axis.BusinessObject.Research;
using Axis.BusinessObject.Core;

namespace Axis.Web.Views.Project
{
    public partial class ucQuestionare : System.Web.UI.UserControl
    {
        CProjectUI projectUI = new CProjectUI();
        protected void Page_Load(object sender, EventArgs e)
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            string ProjectID = "1";
            bool LastOutputValue;
            string LastOutputDescription;
            if (Request.QueryString["ProjectID"] != null)
                ProjectID = Request.QueryString["ProjectID"].ToString();
            if (Page.RouteData.Values["ProjectID"] != null)
                ProjectID = Page.RouteData.Values["ProjectID"] as string;
            projectUI = new CProjectUI(member);
            string result = projectUI.GetUserResponse(ProjectID, out LastOutputValue, out LastOutputDescription);
            StringBuilder scriptServerConfig = new StringBuilder();
            scriptServerConfig.AppendFormat("var ProjectID={0};\n", ProjectID.Trim());
            scriptServerConfig.AppendFormat("var LastOutputValue='{0}';\n", LastOutputValue == true ? 1 : 0);
            scriptServerConfig.AppendFormat("var LastOutputDescription='{0}';\n", LastOutputDescription);            
            scriptServerConfig.AppendFormat("var UserResponse={0};\n", result.Trim());

            CResearchDao cResearchDao = new CResearchDao();


            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("Action", "GET");
            root.SetAttribute("UserID", member.ID.ToString());
            root.SetAttribute("SecrectToken", member.SecrectToken);
            root.SetAttribute("PageIndex", "1");
            root.SetAttribute("RowsPerPage", "999");
            root.SetAttribute("ProjectID", ProjectID);
            root.SetAttribute("ID", ProjectID);
            IList<CProject> list = cResearchDao.GetProject(root.OuterXml);
            if (list != null)
                 if (list.Count > 0)
                {
                    CProject Item = list[0];
                    if (Item.ID != 0)
                    {
                        scriptServerConfig.AppendFormat("var isRequire='{0}';\n", 1);
                        scriptServerConfig.AppendFormat("var HouseholdIncomeRequired='{0}';\n",
                                                        Item.IncomeHouseHoldsRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var CMNDRequired='{0}';\n", Item.CMNDRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var DateOfBirthRequired='{0}';\n",
                                                        Item.DateOfBirthRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var AddressRequired='{0}';\n", Item.AddressRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var EducationIDRequired='{0}';\n",
                                                        Item.EducationRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var JobIDRequired='{0}';\n", Item.JobIDRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var MaritalStatusRequired='{0}';\n",
                                                        Item.MaritalStatusRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var PhoneRequired='{0}';\n",
                                                        Item.PhoneRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var MobileRequired='{0}';\n",
                                                        Item.MobileRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var ProvinceIDRequired='{0}';\n",
                                                        Item.ProvinceIDRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var DistrictRequired='{0}';\n",
                                                      Item.DistrictRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var WardRequired='{0}';\n",
                                                     Item.WardRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var ClassRequired='{0}';\n",
                                                        Item.PersonalIncomeRequired == true ? 1 : 0);
                        scriptServerConfig.AppendFormat("var ProjectName='{0}';\n", Item.Name);

                        scriptServerConfig.AppendFormat("var QuestionCount={0};\n", Item.QuestionCount);
                        //   scriptServerConfig.AppendFormat("var MaritalStatusRequired='{0}';\n", Item.MaritalStatusRequired == true ? 1 : 0);

                    }
                    else

                        scriptServerConfig.AppendFormat("var isRequire='{0}';\n", 1);
                }
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "ServerConfig",
                                     scriptServerConfig.ToString(), true);
          
        }
        protected int GetNumQuestion(string ProjectID)
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", member.ID.ToString());
            root.SetAttribute("SecrectToken", member.SecrectToken);
            root.SetAttribute("PageIndex", "1");
            root.SetAttribute("ID", ProjectID);
            root.SetAttribute("RowsPerPage", "999");
            CResearchDao core = new CResearchDao();
            CCore cCore = new CCore();
            string inputValue = root.OuterXml;
            CResearchDao cResearchDao = new CResearchDao();
            IList<CProject> list = cResearchDao.GetProject(inputValue);
            if (list != null)
                if (list.Count > 0)
                    return list[0].QuestionCount;
            return 99999;
        }
    }
}
